package Loadtext 
{
	import flash.geom.Point;
	import flash.geom.Rectangle;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	import flash.events.Event;
	import flash.utils.ByteArray;
	/**
	 * ...
	 * @author Vượng
	 */

	public class LoadText 
	{
		public 	var myTextLoader:URLLoader ;
		public	var url:URLRequest;
		public var m_textData:String;
		public  var m_textPath:String;
		public  var m_textBase:String;
		public  var m_textRect:String;
		public  var m_textWave:String;
		public  var m_textCoint:String;
		public  var m_textHead:String;
		
		public var m_listData_Path:Array;
		public var m_listData_Base:Array;
		public var m_listData_Rect:Array;
		public var m_listData_Wave:Array;
		public var m_Coint:Number;
		public var m_Head:Number;
		
		public var m_LoadComplete:Boolean;
		public function LoadText(file:String) 
		{
			m_listData_Path = new Array();
			m_listData_Base = new Array();
			m_listData_Rect = new Array();
			m_listData_Wave = new Array();
			m_Coint = new Number();
			m_Head= new Number();
			onLoaded();
		}
		
		private function onLoaded():void 
		{
			var tem:ByteArray = Assets.getTxt();
			m_textData = tem.readUTFBytes(tem.length);
			var _start:String = "LV" +database.instance.m_curStage+""+ (int)((database.instance.m_curLevel + 1)/10)+""+(database.instance.m_curLevel + 1)%10 + "_START";
			var _end:String = "LV"+database.instance.m_curStage+""+(int)((database.instance.m_curLevel + 1)/10)+""+(database.instance.m_curLevel + 1)%10+"_END";
			m_textData=m_textData.substring(m_textData.indexOf(_start) + 10, m_textData.indexOf(_end));
			if (m_textData.indexOf("Path") != -1)
			{
				m_textPath =    m_textData.substring(m_textData.indexOf("Path") + 4, m_textData.indexOf("Rect"));
				m_textRect =	m_textData.substring(m_textData.indexOf("Rect") + 4, m_textData.indexOf("Base"));
				m_textBase =	m_textData.substring(m_textData.indexOf("Base") + 4, m_textData.indexOf("Wave"));	
				m_textWave =	m_textData.substring(m_textData.indexOf("Wave") + 4, m_textData.indexOf("Coin"));
				m_textCoint =	m_textData.substring(m_textData.indexOf("Coin") + 4, m_textData.indexOf("Head"));
				m_textHead =	m_textData.substring(m_textData.indexOf("Head") + 4, m_textData.length);
				while (m_textPath.indexOf(" ") != -1)
				{
					//x
					var index:int = m_textPath.indexOf(" ");
					var _path:Path_Road = new Path_Road();
					_path.x =	Number (m_textPath.substr(0, index));
					m_textPath = m_textPath.substring(index + 1, m_textPath.length);
					//y
					index = m_textPath.indexOf(" ");
					_path.y =	Number (m_textPath.substr(0, index));
					m_textPath = m_textPath.substring(index + 1, m_textPath.length);
					//path
					index = m_textPath.indexOf(" ");
					_path.path =	Number (m_textPath.substr(0, index));
					m_textPath = m_textPath.substring(index + 1, m_textPath.length);
					
					m_listData_Path.push(_path);
				}
				while (m_textRect.indexOf(" ") != -1)
				{
					//x
					index = m_textRect.indexOf(" ");
					var _rect:Rectangle = new Rectangle();
					_rect.x =	Number (m_textRect.substr(0, index));
					m_textRect = m_textRect.substring(index + 1, m_textRect.length);
					//y
					index = m_textRect.indexOf(" ");
					_rect.y =	Number (m_textRect.substr(0, index));
					m_textRect = m_textRect.substring(index + 1, m_textRect.length);
					
					//w
					index = m_textRect.indexOf(" ");
					_rect.width =	Number (m_textRect.substr(0, index));
					m_textRect = m_textRect.substring(index + 1, m_textRect.length);
					
					//h
					index = m_textRect.indexOf(" ");
					_rect.height =	Number (m_textRect.substr(0, index));
					m_textRect = m_textRect.substring(index + 1, m_textRect.length);
					
					m_listData_Rect.push(_rect);
				}
				while (m_textBase.indexOf(" ") != -1)
				{
					//x
					index = m_textBase.indexOf(" ");
					var _base:Point = new Point();
					_base.x =	Number (m_textBase.substr(0, index));
					m_textBase = m_textBase.substring(index + 1, m_textBase.length);
					//y
					index = m_textBase.indexOf(" ");
					_base.y =	Number (m_textBase.substr(0, index));
					m_textBase = m_textBase.substring(index + 1, m_textBase.length);
					
					m_listData_Base.push(_base);
				}
				
				while (m_textWave.indexOf(" ") != -1)
				{
					//x
					index = m_textWave.indexOf(" ");
					var _wave:Wave_Text = new Wave_Text();
					_wave.x =	Number (m_textWave.substr(0, index));
					m_textWave = m_textWave.substring(index + 1, m_textWave.length);
					//y
					index = m_textWave.indexOf(" ");
					_wave.y =	Number (m_textWave.substr(0, index));
					m_textWave = m_textWave.substring(index + 1, m_textWave.length);
					
					//path
					index = m_textWave.indexOf(" ");
					_wave.path =	Number (m_textWave.substr(0, index));
					m_textWave = m_textWave.substring(index + 1, m_textWave.length);
					
					//wave
					index = m_textWave.indexOf(" ");
					_wave.wave =	Number (m_textWave.substr(0, index));
					m_textWave = m_textWave.substring(index + 1, m_textWave.length);
					
					//id
					index = m_textWave.indexOf(" ");
					_wave.id =	Number (m_textWave.substr(0, index));
					m_textWave = m_textWave.substring(index + 1, m_textWave.length);
					
					m_listData_Wave.push(_wave);
				}
					index = m_textCoint.indexOf(" ");
					m_Coint =	Number (m_textCoint.substr(0, index));
					m_Head=Number (m_textHead.substr(0, index));
				
				m_LoadComplete = true;
			}

			
		}
		
		
	}

}